<template>
    <div class="contain-box">
        <div class="box-header">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>商品管理</el-breadcrumb-item>
                <el-breadcrumb-item to="/salesSlip" replace>商品</el-breadcrumb-item>
                <el-breadcrumb-item>新增商品</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <div class="box-body">
            <div class="box-nav">基础信息</div>
            <div class="body-set">
                <el-input v-model="addObj.goodCode" placeholder="请输入商品编码" style="width: 250px;margin-right: 10px"
                          size="small"></el-input>
                <!--                <el-input placeholder="请输入条形码" style="width: 250px;margin-right: 10px" size="small"></el-input>-->
                <el-input v-model="addObj.name" placeholder="请输入商品名称" style="width: 250px;margin-right: 10px"
                          size="small"></el-input>

                <el-input v-model="addObj.inventory" placeholder="请输入商品库存" style="width: 250px;margin-right: 10px"
                          size="small"></el-input>
            </div>
            <div class="body-set">

                <el-select v-model="addObj.unitId" placeholder="请选择计量单位" size="small"
                           style="width: 250px;margin-right: 10px">
                    <el-option v-for="item in measurementUnit" :key="item.id" :label="item.name"
                               :value="item.id"></el-option>
                </el-select>

                <!--                <el-date-picker-->
                <!--                        style="width: 300px;margin-right: 10px"-->
                <!--                        size="small"-->
                <!--                        type="daterange"-->
                <!--                        range-separator="至"-->
                <!--                        start-placeholder="上架日期"-->
                <!--                        end-placeholder="上架日期">-->
                <!--                </el-date-picker>-->
                <el-cascader @change="changeNode" clearable :show-all-levels="false" :props="optionProps" v-model="addObj.goodTypeId"
                             :options="goodTypeList" placeholder="请输入商品分类"
                             style="width: 250px;margin-right: 10px"
                             size="small"></el-cascader>

                <el-input v-model="addObj.price" placeholder="请输入商品价格" style="width: 250px;margin-right: 10px"
                          size="small"></el-input>

            </div>



            <div class="body-set">
                <div style="width: 300px;display: flex;align-items: center">
                    <span style="margin: 0 10px;color: #606266;font-size: 13px;">是否上架: </span>
                    <el-switch
                            v-model="addObj.status"
                            :inactive-value="0"
                            :active-value="1"
                            inactive-text="否"
                            active-text="是"
                            active-color="#13ce66"
                            inactive-color="#ff4949">
                    </el-switch>
                </div>
            </div>

        </div>
        <div class="box-body">
            <div class="box-nav">规格信息</div>
            <div class="body-set">
                <el-select v-model="addObj.goodAttributeId" placeholder="请选择商品属性" size="small"
                           style="width: 250px;margin-right: 10px">
                    <el-option v-for="item in attributeList" :key="item.id" :label="item.attributeName"
                               :value="item.id"></el-option>
                </el-select>
            </div>
            <!--            <div style="width: 100%;margin-top: 20px">-->
            <!--                <el-table-->
            <!--                        :data="tableData"-->
            <!--                        stripe-->
            <!--                        border-->
            <!--                        style="width: 100%">-->
            <!--                    <el-table-column prop="name" label="净含量" align="center"></el-table-column>-->
            <!--                    <el-table-column label="零售价(元)" align="center">-->
            <!--                        <template slot-scope="scope">-->
            <!--                            <el-input v-model="scope.row.money" placeholder="零售价" size="small"></el-input>-->
            <!--                        </template>-->
            <!--                    </el-table-column>-->
            <!--                    <el-table-column label="最高库存预警值" align="center">-->
            <!--                        <template slot-scope="scope">-->
            <!--                            <el-input v-model="scope.row.tall" placeholder="最高库存预警值" size="small"></el-input>-->
            <!--                        </template>-->
            <!--                    </el-table-column>-->
            <!--                    <el-table-column label="最低库存预警值" align="center">-->
            <!--                        <template slot-scope="scope">-->
            <!--                            <el-input v-model="scope.row.low" placeholder="最低库存预警值" size="small"></el-input>-->
            <!--                        </template>-->
            <!--                    </el-table-column>-->
            <!--                </el-table>-->
            <!--            </div>-->
        </div>
        <!--        <div class="box-body">-->
        <!--            <div class="box-nav">库存期初设置</div>-->
        <!--            <div class="body-set">-->
        <!--                <el-button type="primary" size="small">新增库存设置</el-button>-->
        <!--            </div>-->
        <!--            <div style="width: 100%;margin-top: 20px">-->
        <!--                <el-table-->
        <!--                        :data="setData"-->
        <!--                        stripe-->
        <!--                        border-->
        <!--                        style="width: 100%">-->
        <!--                    <el-table-column type="index" label="序号" width="100" align="center"></el-table-column>-->
        <!--                    <el-table-column label="仓库" align="center">-->
        <!--                        <template slot-scope="scope">-->
        <!--                            <el-select placeholder="请选择仓库" size="small">-->
        <!--                                <el-option label="仓库1" value="1"></el-option>-->
        <!--                                <el-option label="仓库2" value="2"></el-option>-->
        <!--                                <el-option label="仓库3" value="3"></el-option>-->
        <!--                                <el-option label="仓库4" value="4"></el-option>-->
        <!--                            </el-select>-->
        <!--                        </template>-->
        <!--                    </el-table-column>-->
        <!--                    <el-table-column label="期初库存" align="center">-->
        <!--                        <template slot-scope="scope">-->
        <!--                            <el-input placeholder="期初库存" size="small"></el-input>-->
        <!--                        </template>-->
        <!--                    </el-table-column>-->
        <!--                    <el-table-column label="单个库存成本" align="center">-->
        <!--                        <template slot-scope="scope">-->
        <!--                            <el-input placeholder="库存成本" size="small"></el-input>-->
        <!--                        </template>-->
        <!--                    </el-table-column>-->
        <!--                    <el-table-column label="库存总成本(元)" align="center">-->
        <!--                        <template slot-scope="scope">-->
        <!--                            <el-input placeholder="库存总成本" size="small"></el-input>-->
        <!--                        </template>-->
        <!--                    </el-table-column>-->
        <!--                    <el-table-column label="操作" align="center">-->
        <!--                        <template slot-scope="scope">-->
        <!--                            <el-button type="text" size="mini">删 除</el-button>-->
        <!--                        </template>-->
        <!--                    </el-table-column>-->

        <!--                </el-table>-->
        <!--            </div>-->
        <!--        </div>-->
        <div class="box-body">
            <div class="box-nav">商品图片</div>
            <div style="margin-top: 20px;width: 100%;">
                <el-upload
                        class="upload-demo"
                        :action="api+'/unitInfo/uploadFile'"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :on-success="onSuccess"
                        :headers="header"
                        :limit="8"
                        :file-list="fileList"
                        list-type="picture">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">单张图片大小不超过5M，支持格式JPG、PNG，最多上传8张</div>
                </el-upload>
            </div>
        </div>
        <div class="box-submit">
            <div class="submit-left">
            </div>
            <div>
                <el-button size="small" @click="goBack">取 消</el-button>
                <el-button @click="submitAdd" type="primary" size="small" style="background-color: #1abc9c">提交审核
                </el-button>
            </div>
        </div>
    </div>
</template>

<script>
    import common from "../utils/common";

    export default {
        name: "AddGood",
        data() {
            return {
                api: null,
                header: {},
                optionProps: {
                    value: "id",
                    label: "name",
                    children: "goodTypeList",
                    checkStrictly: true
                },
                addObj: {
                    goodCode: null,
                    goodTypeId: null,
                    name: null,
                    mainCoverUrl: null,
                    coverUrlList: [],
                    unitId: null,
                    inventory: null,
                    statut: 0,
                    goodAttributeId: null,
                    price: null
                },
                tableData: [
                    {
                        name: '250ml',
                        money: '',
                        tall: '',
                        low: ''
                    },
                    {
                        name: '500ml',
                        money: '',
                        tall: '',
                        low: ''
                    },
                    {
                        name: '750ml',
                        money: '',
                        tall: '',
                        low: ''
                    },
                ],
                setData: [
                    {
                        name: 1
                    }
                ],
                measurementUnit: [],
                goodTypeList: [],
                attributeList: [],
                fileList: [],
            }
        },
        created() {
            this.getAllMeasurementUnit();
            this.geAllGoodType()
            this.getGoodAttrbute()
            this.api = common.requestUrl
            this.header.token = sessionStorage.getItem('tokenValue')
        },
        methods: {
            changeNode(){
              let arr= this.addObj.goodTypeId
                this.addObj.goodTypeId = null
                this.addObj.goodTypeId = arr[arr.length-1]
            },
            submitAdd(){
              this.axios.post('/good/save',this.addObj).then(res=>{
                  if(res.data.code===200){
                      this.goBack()
                      this.$message.success('新增成功')
                  }
              })
            },
            onSuccess(response, file, fileList) {
                this.addObj.coverUrlList = []
                if (response.code === 200) {
                    // this.addObj.certificateUrl = response.data
                    this.$message.success(response.message)
                } else {
                    this.$message.warning(response.message)
                }
                for (let i = 0; i < fileList.length; i++) {
                    this.addObj.coverUrlList.push(fileList[i].response.data)
                }
            },
            //查询所有商品属性
            getGoodAttrbute() {
                this.axios.get('/goodAttribute/findAll').then(res => {
                    if (res.data.code === 200) {
                        this.attributeList = res.data.data;
                    }
                })
            },
            //查询所有商品分类
            geAllGoodType() {
                this.axios.get('/goodType/findAll').then(res => {
                    if (res.data.code === 200) {
                        this.goodTypeList = res.data.data
                    }
                })
            },
            //查询所有计量单位
            getAllMeasurementUnit() {
                this.axios.post('/goodMeasurementUnit/findAll?name=1').then(res => {
                    if (res.data.code === 200) {
                        this.measurementUnit = res.data.data
                    }
                })
            },
            goBack() {
                this.$router.replace('/commodity')
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
                this.addObj.coverUrlList = []
                for (let i = 0; i < fileList.length; i++) {
                    this.addObj.coverUrlList.push(fileList[i].response.data)
                }
            },
            handlePreview(file) {
                console.log(file);
            }
        }
    }
</script>

<style scoped lang="less">
    .contain-box {
        width: 100%;
        height: 100%;

        /deep/ .distpicker-address-wrapper {
            select {
                width: 32% !important;
                padding: 0px 10px !important;
                height: 35px !important;
                font-size: 12px !important;
                line-height: 35px !important;
                border-radius: 5px;
            }
        }

        .dialog-box {
            width: 100%;
        }

        .dialog-page {
            margin-top: 20px;
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: flex-end;

            /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active {
                background-color: #1abc9c;
            }
        }

        /deep/ .el-dialog__body {
            padding: 10px 20px;
        }

        .box-header {
            width: 100%;
            height: 50px;
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }

        .box-body {
            width: 100%;
            border-radius: 5px;
            padding: 20px;
            box-sizing: border-box;
            box-shadow: 0 0 5px 0 #e4e4e4;

            .box-nav {
                width: 100%;
                height: 40px;
                line-height: 40px;
                padding-left: 10px;
                box-sizing: border-box;
                font-size: 14px;
                color: #666;
                border-left: 5px solid #1abc9c;
                background-color: #f9f9f9;
            }

            .body-top {
                width: 100%;
                height: 50px;
                text-align: center;
                line-height: 50px;
                border-bottom: 1px solid #e4e4e4;
                font-size: 20px;
                color: #666;
            }

            .body-set {
                margin-top: 20px;
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: flex-start;
            }

            .body-add {
                margin-top: 20px;
                width: 100%;
            }

            .body-table {
                margin-top: 20px;
                width: 100%;
            }

            .body-message {
                margin-top: 20px;
                width: 100%;

                .message-box {
                    margin-top: 20px;
                    display: flex;
                    align-items: center;
                    cursor: pointer;

                    .message-img {
                        margin-left: 20px;
                        width: 20px;
                        height: 20px;
                    }

                    .message-son {
                        margin-right: 15px;
                        font-size: 14px;
                    }
                }
            }
        }

        .box-submit {
            margin-top: 20px;
            width: 100%;
            border-radius: 5px;
            padding: 20px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 0 5px 0 #e4e4e4;

            .submit-left {
                display: flex;
                font-size: 16px;

                p {
                    margin-right: 20px;

                    span {
                        color: red;
                        font-weight: bold;
                    }
                }
            }
        }
    }
</style>
